﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Standards;
namespace PH.Data.Standards
{
    public class SubjectCommand
    {
        public static SubjectModel GetSubject(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var data = db.DataContext.Subjects.FirstOrDefault(p => p.ID == id);
                    if (data != null)
                    {
                        var model = Convertor.ConvertTo<SubjectModel, Subject>(data);
                        return model;
                    }
                }
            }
            return null;
        }

        public static SubjectModel CreateNew(SubjectModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var data = Convertor.ConvertFrom<Subject, SubjectModel>(model);
                    db.DataContext.Subjects.Add(data);
                    db.DataContext.SaveChanges();
                    model.ID = data.ID;
                }
            }
            return model;
        }

        public static void Update(SubjectModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var data = db.DataContext.Subjects.FirstOrDefault(p => p.ID == model.ID);
                    if (data != null)
                    {
                        SaveCheck(model, db);
                        Convertor.UpdateValue(model, data);
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }

        private static void SaveCheck(SubjectModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.SubjectName))
                throw new Utils.PhException("请提供项目名称");
        }

        public static SubjectCollection GetSubjects(SubjectQuery sea)
        {
            var result = new SubjectCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Subject> q = db.DataContext.Subjects;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.SubjectName.Contains(sea.Key));
                    if (sea.GradeCode > 0)
                    {
                        var grade = sea.GradeCode.ToString();
                        q = q.Where(p => p.AdapterGrade.Contains(grade));
                    }
                    //if (sea.Category != null && sea.Category.Length > 0)
                    //    q = q.Where(p => sea.Category.Contains(p.Category));
                        //q = q.Where(p => p.Category==2 || p.Category==3); //此代码暂时用着，因为bug解决不了。待处理
                }
                
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<SubjectModel, Subject, SubjectCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }

        public static void Delete(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Subject>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
